package com.bw.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * IO流工具类
 *      文件解析
 */
public class IOUtils {

    /**
     * IO流解析文件
     * @param path  文件路径
     * @param spilt 拆分符号
     * @return
     */
    public static List<String[]> readFile(String path,String spilt){

        List<String[]> list = new ArrayList<String[]>();

            BufferedReader bufferedReader = null;
               try {
                   bufferedReader  = new BufferedReader(new FileReader(path));
                   String str = null;
                   while((str = bufferedReader.readLine())!=null){
                       //  \s+   使用一个或多个空格分割字符串
                       String[] split = str.split(spilt);
                        list.add(split);
                   }
               }catch (Exception e){
                    e.printStackTrace();
               }finally {
                  try{
                      bufferedReader.close();
                  }catch (Exception e){
                      e.printStackTrace();
                  }
               }
               return list;
    }


    /*
    public static void main(String[] args) throws Exception {
        //1. 获取Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = ac.getBean(UserMapper.class);
        List<String[]> list = IOUtils.readFile("db.txt", "\\s+");
        for (String[] strings : list) {
            User user = new User(Integer.parseInt(strings[0]),strings[1],strings[2]);
            System.out.println(user);
            mapper.insertUser(user);
        }        
    }*/

}
